package com.liziba.demo.slice;

import com.liziba.demo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;


public class MainAbilitySlice extends AbilitySlice implements Component.ClickedListener, Component.DoubleClickedListener {

    /** DirectionalLayout控件 */
    DirectionalLayout directionalLayout;
    /** Image控件 */
    Image image;
    /** 是否点赞  true 代表点赞 false 代表取消点赞 */
    Boolean like = false;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);

        // 获取DirectionalLayout控件
        directionalLayout = (DirectionalLayout) this.findComponentById(ResourceTable.Id_dl);
        // 获取Image控件
        image = (Image) this.findComponentById(ResourceTable.Id_img);

        // 注册双击事件
        directionalLayout.setDoubleClickedListener(this);
        // 注册单击事件
        image.setClickedListener(this);
    }

    /**
     * 单击事件方法
     *
     * @param component
     */
    @Override
    public void onClick(Component component) {
        // 单击修改Image图片
        if (component.getId() == ResourceTable.Id_img) {
            like = !like;
            image.setImageAndDecodeBounds(like ? ResourceTable.Media_red : ResourceTable.Media_white);
        }
    }

    /**
     * 双击事件方法
     *
     * @param component
     */
    @Override
    public void onDoubleClick(Component component) {
        // 双击修改Image图片，如果未点赞，则修改为红色小红心，如果已点赞则不处理
        if (component.getId() == ResourceTable.Id_dl && !like) {
            image.setImageAndDecodeBounds(ResourceTable.Media_red);
        }
    }


    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
